package cn.stylefeng.guns.cloud.workflow.modular.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.cloud.libs.context.auth.LoginContext;
import cn.stylefeng.guns.cloud.libs.scanner.annotation.PostResource;
import cn.stylefeng.guns.cloud.libs.scanner.stereotype.ApiResource;
import cn.stylefeng.guns.cloud.model.exp.ServiceException;
import cn.stylefeng.guns.cloud.model.page.PageResult;
import cn.stylefeng.guns.cloud.model.web.response.ResponseData;
import cn.stylefeng.guns.cloud.workflow.modular.entity.Myleave;
import cn.stylefeng.guns.cloud.workflow.modular.model.params.MyLeaveParam;
import cn.stylefeng.guns.cloud.workflow.modular.provider.WorkflowApiProvider;
import cn.stylefeng.guns.cloud.workflow.modular.service.MyleaveService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.ActivitiException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 请假审批
 *
 * @author fengshuonan
 * @Date 2019-08-20 08:53:25
 */
@RestController
@ApiResource(name = "请假申请接口", path = "/leave")
@Api(tags = "请假申请接口")
@Slf4j
public class MyLeaveController extends WorkflowApiProvider {

    @Autowired
    private MyleaveService myleaveService;

    /**
     * 发起请假
     *
     * @author fengshuonan
     * @Date 2019-08-20
     */
    @PostResource(name = "发起请假", path = "/startLeave")
    @ApiOperation(value = "发起请假")
    public ResponseData addItem(@RequestBody MyLeaveParam myleaveParam) {

        String account = LoginContext.me().getLoginUser().getAccount();
        String username = LoginContext.me().getLoginUser().getName();

        try {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("请假人员", username);
            map.put("开始时间", myleaveParam.getStarttime());
            map.put("结束时间", myleaveParam.getEndtime());
            map.put("请假时长", myleaveParam.getWhenlong());
            map.put("请假类型", myleaveParam.getType());
            map.put("请假事由", myleaveParam.getReason());

            //指派代理人为当前用户
            map.put("username", account);

            //启动流程实例(请假单流程)通过KEY
            super.start("key_leave", 123L, myleaveParam, map);

            myleaveParam.setUsername(account);
            this.myleaveService.add(myleaveParam);

        } catch (ActivitiException e) {
            log.error("无可用流程，请先导入或新建流程", e);
            throw new ServiceException(500, "无可用流程，请先导入或新建流程");
        }

        return ResponseData.success();
    }

    /**
     * 删除请假
     *
     * @author fengshuonan
     * @Date 2019-08-20
     */
    @PostResource(name = "删除请假", path = "/delete")
    @ApiOperation(value = "删除请假")
    public ResponseData delete(@RequestBody MyLeaveParam myleaveParam) {
        this.myleaveService.delete(myleaveParam);
        return ResponseData.success();
    }

    /**
     * 批量删除接口
     *
     * @author fengshuonan
     * @Date 2019-08-20
     */
    @PostResource(name = "删除请假", path = "/batchDelete")
    @ApiOperation(value = "删除请假")
    public ResponseData batchDeleteByIds(@RequestParam("ids") List<String> ids) {
        if (ObjectUtil.isEmpty(ids)) {
            return ResponseData.success("无选中项");
        }
        this.myleaveService.batchDeleteByIds(ids);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author fengshuonan
     * @Date 2019-08-20
     */
    @PostResource(name = "查看详情接口", path = "/detail")
    @ApiOperation(value = "查看详情接口")
    public ResponseData detail(@RequestBody MyLeaveParam myleaveParam) {
        Myleave detail = this.myleaveService.getById(myleaveParam.getMyleaveId());
        return ResponseData.success(detail);
    }

    /**
     * 请假单查询列表
     *
     * @author fengshuonan
     * @Date 2019-08-20
     */
    @PostResource(name = "请假单查询列表", path = "/list")
    @ApiOperation(value = "请假单查询列表")
    public ResponseData list(MyLeaveParam myleaveParam) {
        Page<Map<String, Object>> pageBySpec = this.myleaveService.findPageBySpec(myleaveParam);
        return ResponseData.success(new PageResult<>(pageBySpec));
    }

    /**
     * 获取请假类型
     *
     * @author fengshuonan
     * @Date 2019-08-20
     */
    @PostResource(name = "获取请假类型", path = "/getLeaves")
    @ApiOperation(value = "获取请假类型")
    public ResponseData getLeaves() {

        HashMap<String, Object> map = new HashMap<>();
        map.put("1", "事假");
        map.put("2", "病假");
        map.put("3", "产假");

        return ResponseData.success(map);
    }

}


